package com.kate.mall.order.price;

import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.kate.mall.order.dto.GoodsSkuDTO;
import com.kate.mall.order.dto.OrderItemDTO;
import com.kate.mall.order.dto.PromotionActivityDTO;
import com.kate.mall.order.feign.ProductFeignClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 赠品促销类型的促销活动的价格计算组件
 *
 * @author kate2chan
 */
@Component
@Slf4j
public class DirectGiftPromotionActivityCalculator
        extends AbstractGiftPromotionActivityCalculator
        implements PromotionActivityCalculator {

    /**
     * 商品中心接口
     */
    @Autowired
    private ProductFeignClient productFeignClient;

    @Override
    public PromotionActivityResult calculate(OrderItemDTO item,
                                             PromotionActivityDTO promotionActivity) {

        JSONObject rule = JSONUtil.parseObj(promotionActivity.getRule());

        JSONArray giftGoodsSkuIds = rule.getJSONArray("giftGoodsSkuIds");

        PromotionActivityResult result = new PromotionActivityResult();
        for (int i = 0; i < giftGoodsSkuIds.size(); i++) {
            Integer goodsSkuId = giftGoodsSkuIds.getInt(i);
            GoodsSkuDTO goodsSku = MapUtil.get(productFeignClient.getGoodsSkuById(goodsSkuId), "goodsProduct", GoodsSkuDTO.class);
            result.getOrderItems().add(createOrderItem(goodsSku));
        }
        log.info("====赠品促销活动价格 计算器，计算活动减免价为：{}====",result.getDiscountAmount());
        return result;
    }


}